#include "widget.h"
#include "ui_widget.h"

#include <iostream>
#include <cstring>
#include <cctype>
#include <QRegExp>
#include <QRegExpValidator>
#include <QButtonGroup>
#include <QTimer>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    v(3,false)
{
    ui->setupUi(this);

    //le_name 对用户名输入就行设置
    ui->le_name->setPlaceholderText("input user name");
    ui->le_name->setClearButtonEnabled(true);

    //le_phone 设置电话
    //ui->le_phone->setPlaceholderText("input phone code");
    ui->le_phone->setClearButtonEnabled(true);
    //ui->le_phone->setInputMask("000-0000-0000"); //设置电话的格式为 3数字-4数字-4数字
    QRegExp regexp("^1\\d{10}$"); //表示第一个数字必须为1 后面有十个数字 然后结尾
    ui->le_phone->setValidator(new QRegExpValidator(regexp));

    //设置性别选项 默认为男
    ui->rb_male->setChecked(true);
    //性别设置到组
    QButtonGroup* qbg = new QButtonGroup(this);
    qbg->addButton(ui->rb_male);
    qbg->addButton(ui->rb_famale);

    //设置密码 第一次输入密码判断强度
    //设置密码遮掩
    ui->le_passwd->setPlaceholderText("input password");
    ui->le_passwd2->setPlaceholderText("enter your password again");
    ui->le_passwd->setEchoMode(QLineEdit::Password);
    ui->le_passwd2->setEchoMode(QLineEdit::Password);

    //设置定时器 定期检查按钮是否可以使用
    QTimer* timer = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&Widget::pbtEnable);
    timer->start();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::pbtEnable()
{
    if(v[0] && v[1] && v[2]) ui->pbt->setEnabled(true);
    else ui->pbt->setEnabled(false);
}

int Widget::judge(const QString& s)
{
    //小写和大写和数字和特殊字符
    bool a = false,b = false,d = false,e = false;
    int cnt = 0;
    for(const QChar& c:s)
    {
        if(c>='a' && c<='z') a = true;
        if(c>='A' && c<='Z') b = true;
        if(c>='0' && c<='9') d = true;
        if(!(c>='a' && c<='z') && !(c>='A' && c<='Z') && !(c>='0' && c<='9')) e = true;
    }

    return a+b+d+e;
}


void Widget::on_le_phone_textEdited(const QString &arg1)
{
    (void)arg1;
    QString s = ui->le_phone->text();
    int ret = 0;
    //判断匹配是否成功
    if(ui->le_phone->validator()->validate(s,ret) == QValidator::Acceptable) v[1] = true;
    else v[1] = false;
}

void Widget::on_le_name_textEdited(const QString &arg1)
{
    if(arg1.size()) v[0] = true;
    else v[0] = false;
}

void Widget::compare()
{
    QString p1 = ui->le_passwd->text();
    QString p2 = ui->le_passwd2->text();
    if(!p1.isEmpty() && !p2.isEmpty() && p1==p2) ui->lpasswd2->setText("Pass"),v[2] = true;
    else if(!p2.isEmpty()) ui->lpasswd2->setText("No Pass"),v[2] = false;
    else ui->lpasswd2->setText(""),v[2]=false;
}

//第一次输入密码难度校验以及比对两次输入
void Widget::on_le_passwd_textEdited(const QString &arg1)
{
    if(arg1.size() == 0)
    {
        ui->lpasswd->setText("");
        return;
    }
    if(arg1.size() < 8)
    {
        ui->lpasswd->setStyleSheet("color:#000000;");
        ui->lpasswd->setText("password length too short");
        return;
    }

    switch(judge(arg1))
    {
    case 1:
    {
        ui->lpasswd->setText("low");
        ui->lpasswd->setStyleSheet("color:#FF0000;");
        break;
    }
    case 2:
    {
        ui->lpasswd->setText("middle");
        ui->lpasswd->setStyleSheet("color:#FFFF00;");
        break;
    }
    case 3:
    {
        ui->lpasswd->setText("high");
        ui->lpasswd->setStyleSheet("color:#90EE90;");
        break;
    }
    case 4:
    {
        ui->lpasswd->setText("Very high");
        ui->lpasswd->setStyleSheet("color:#008000;");
        break;
    }
    default:
    {
        ui->lpasswd->setText("");
        ui->lpasswd->setStyleSheet("color:#000000;");
        break;
    }
    }
    compare();
}


//第二次输入密码比对是否输入一致
void Widget::on_le_passwd2_textEdited(const QString &arg1)
{
    compare();
}

//按键按下显示密码 松开遮盖密码
void Widget::on_pbt_passwd_pressed()
{
    ui->le_passwd->setEchoMode(QLineEdit::Normal);
}

void Widget::on_pbt_passwd_released()
{
    ui->le_passwd->setEchoMode(QLineEdit::Password);
}

void Widget::on_pbt_passwd2_pressed()
{
    ui->le_passwd->setEchoMode(QLineEdit::Normal);
}

void Widget::on_pbt_passwd2_released()
{
    ui->le_passwd->setEchoMode(QLineEdit::Password);
}

void Widget::on_pbt_clicked()
{
    qDebug() << "------------------------------";
    qDebug() << "用户: " << ui->le_name->text();
    qDebug() << "电话: " << ui->le_phone->text();
    qDebug() << "性别: " << (ui->rb_male->isChecked()?"male":"famale");
    qDebug() << "密码: " << ui->le_passwd->text();
    qDebug() << "------------------------------";
}
